﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DLLTest.OpertionClass;
using DHNetSDK;

namespace DLLTest
{
    public partial class frm_AutoSplitWall : Form
    {
        #region 属性

        /// <summary>
        /// 获取所有已分割的窗口
        /// </summary>
        public List<Panel> PanelList { get; private set; }

        #endregion

        #region 变量

        Action<int> removeWallAction; // 销毁大屏

        /// <summary>
        /// 使用扩展分屏类
        /// </summary>
        PanelOpertionExt panelopertion = new PanelOpertionExt();

        #endregion

        #region 构造函数

        private frm_AutoSplitWall()
        {
            InitializeComponent();
        }

        public frm_AutoSplitWall(Action<int> removeWall)
            : this()
        {
            this.removeWallAction = removeWall;
        }

        #endregion

        #region 事件

        private void frm_AutoSplitWall_Load(object sender, EventArgs e)
        {
            picPlayMain0.Left = picPlayMain0.Top = 0;
            panelopertion.IsHideBorder = true; // 单击时隐藏边框线
            PanelList = panelopertion.SplitScreen(frm_Main.PANEL_COUNT, picPlayMain0, CloseForm); // 默认分 4 屏            
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            CloseForm();
        }

        #endregion

        #region 公开的方法

        /// <summary>
        /// 关闭窗口
        /// </summary>
        public void CloseForm()
        {
            if (this.Tag != null && PanelOpertion.PanelCurrentList != null &&
                PanelOpertion.PanelCurrentList.Count > 0)
            {
                string[] mainFormPanelInfo = this.Tag.ToString().Split(',');
                int mainFormPanelIndex = int.Parse(mainFormPanelInfo[0]); // 主界面中的起始窗口序号
                int mainFormPanelCount = int.Parse(mainFormPanelInfo[1]); // 主界面中的窗口个数
                int wallIndex = int.Parse(mainFormPanelInfo[2]); // 大屏位置序号
                int count = 0; // 记录循环次数

                foreach (Panel singlePanel in PanelList)
                {
                    if (count >= mainFormPanelCount)
                        break;

                    PanelOpertion.PanelCurrentList[mainFormPanelIndex + count].Enabled = true;
                    PanelOpertion.PanelCurrentList[mainFormPanelIndex + count].BackColor = Color.Black;

                    if (singlePanel.Tag != null &&
                        singlePanel.Tag.ToString().Split(',').Length == 7)
                    {
                        // a. 先停止所有上墙窗口中的视频
                        string[] leafTag = singlePanel.Tag.ToString().Split(',');
                        DHClient.DHStopRealPlay(Convert.ToInt32(leafTag[6])); // 停止实时监控，需要传递实时监控句柄

                        // b. 再播放主窗口中的视频
                        PanelOpertion.PanelCurrentList[mainFormPanelIndex + count].Tag = singlePanel.Tag.ToString().Substring(0,
                            singlePanel.Tag.ToString().LastIndexOf(',')); // 去 realPlayHandle

                        string[] mainFormPanelTag = PanelOpertion.PanelCurrentList[mainFormPanelIndex + count].Tag.ToString().Split(',');
                        int pRealPlayHandle = DHClient.DHRealPlay(int.Parse(mainFormPanelTag[4]),
                            Convert.ToInt32(mainFormPanelTag[5]), PanelOpertion.PanelCurrentList[mainFormPanelIndex + count].Handle); //只取数据不播放

                        PanelOpertion.PanelCurrentList[mainFormPanelIndex + count].Tag = string.Format("{0},{1}", PanelOpertion.PanelCurrentList[mainFormPanelIndex + count].Tag, pRealPlayHandle); // 得到最新的 MainFormPanelTag: ip,port,user,pwd,loginID,channel,realPlayHandle
                    }

                    count++;
                }

                // c. 销毁主界面中对应大屏
                if (this.removeWallAction != null)
                {
                    this.removeWallAction(wallIndex);
                }
            }

            // d. 关闭窗口
            this.Close();
        }

        #endregion
    }
}
